/**
 * Created by Panda on 16/01/2017
 */
(function () {
    'use strict';
    angular
        .module('com.akxx.aqzyk')
        .controller('scientificController', scientificController);


    /** @ngInject */
    function scientificController($state,_Scientific,SweetAlert) {
        var vm = this;
        vm.newNav =false;
        vm.newAchievement =false;
        vm.overlay =false;
        vm.isReadonly = false;
        vm.topNavItem = {};
        vm.AchievementItem = {};
        getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        function getTopNavList(param) {
            _Scientific.ClassifyList.get(param).then(function (res) {
                vm.topNavs = res.data;
                vm.active = vm.topNavs[0].id;
                var searchCode = vm.topNavs[0].code;
                getAchievementInfo({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc","q[catalog]":searchCode});
            });
        };

        function getAchievementInfo(param){
            _Scientific.AchievementList.get(param).then(function (res) {
                if(res.success){
                    vm.items = res.data;
                }else{
                    SweetAlert.swal("连接失败");
                }
            });
        }

        vm.topNavClickEvent = function (id) {
            vm.active = id;
            for(var i=0;i<vm.topNavs.length;i++){
                if(vm.topNavs[i].id == id){
                    var searchCode = vm.topNavs[i].code;
                    console.log(searchCode);
                    getAchievementInfo({async: true,sortExpr:"name desc","q[catalog]":searchCode});
                }
            }
            // vm.active = id;
            // for(var i=0;i<vm.topNavs.length;i++){
            //     if(vm.topNavs[i].id == id){
            //         vm.items = vm.topNavs[i].curriculaSysList;
            //     }
            // }
        };

        vm.closeNav = function (id) {
            var param = {id:id};
            _Scientific.DelClassify.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("删除成功");
                }
            });
        };
        //新建tab事件
        vm.addNavEvent = function(){
            vm.newNav = true;
            vm.overlay = true;
            vm.saveNavType = "new";
        }
        // 编辑tab事件
        vm.editNavEvent = function (id) {
            var param={id:id}
            _Scientific.VisitClassify.get(param).then(function (res) {
                if(res.success){
                    vm.topNavItem = res.data;
                    vm.newNav = true;
                    vm.overlay = true;
                    vm.saveNavType = "edit";
                    vm.saveNavId = id;
                }
            });
        }

        //关闭弹窗
        vm.closeFrame = function(){
            vm.newNav = false;
            vm.newAchievement = false;
            vm.overlay = false;
            vm.isReadonly = false;
        };

        // 保存
        vm.saveTopNav = function () {
            if(vm.saveNavType = "new"){
                var param = vm.topNavItem;
                _Scientific.AddClassify.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功");
                        vm.newNav =false;
                    }
                });
            }else if(vm.saveNavType = "edit"){
                var param = vm.topNavItem;
                param.id = vm.saveNavId;
                _Scientific.EditClassify.get(param).then(function (res) {
                    if (res.success) {
                        SweetAlert.swal("编辑成功")
                        vm.newNav =false;
                    }
                });
            }
            getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }

        // 链接保存
        vm.saveAchievement = function () {
            if(vm.saveAchType = "new"){
                var param = vm.AchievementItem;
                _Scientific.AddAchievement.get(param).then(function (res) {
                    if(res.success){
                        SweetAlert.swal("新建成功");
                        vm.newAchievement =false;
                    }
                });
            }else if(vm.saveAchType = "edit"){
                var param = vm.AchievementItem;
                param.id = vm.saveAchId;
                _Scientific.EditAchievement.get(param).then(function (res) {
                    if (res.success) {
                        SweetAlert.swal("编辑成功")
                        vm.newAchievement =false;
                    }
                });
            }
            getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
        }

        //划过事件
        vm.itemShow=function(id){
            vm.isShow = id;
            console.log(vm.isShow);
        }
        vm.itemHide=function(id){
            vm.isShow = "";
        }

        //新建成果事件
        vm.itemAdd = function(){
            vm.newAchievement = true;
            vm.overlay = true;
            vm.saveAchType = "new";
        }
        // 编辑链接事件
        vm.itemEdit = function (id) {
            var param={id:id}
            _Scientific.VisitAchievement.get(param).then(function (res) {
                if(res.success){
                    vm.AchievementItem = res.data;
                    vm.newAchievement = true;
                    vm.overlay = true;
                    vm.saveAchType = "edit";
                    vm.saveAchId = id;
                }
            });
        }
        // 查看链接事件
        vm.itemVisit = function (id) {
            var param={id:id}
            _Scientific.VisitAchievement.get(param).then(function (res) {
                if(res.success){
                    vm.AchievementItem = res.data;
                    vm.newAchievement = true;
                    vm.overlay = true;
                    vm.isReadonly = true;
                }
            });
        }
        //删除成果事件
        vm.itemDel = function(id){
            var param={id:id}
            _Scientific.DelAchievement.get(param).then(function (res) {
                if(res.success){
                    SweetAlert.swal("删除成功！");
                    getTopNavList({async: true,pageSize:0,pageIndex:25,sortExpr:"name desc"});
                    // for(var i=0;i<vm.items.length;i++){
                    //     if(vm.items[i].id == id){
                    //         vm.items.splice(i,1);
                    //
                    //     }
                    // }
                }else{
                    console.log("失败")
                }
            });
        }

        //导航事件
        vm.topNavShow = function(id){
            vm.topNavActive = id;
        }
        vm.topNavHide = function(){
            vm.topNavActive = "";
        }
    }
})();

